package com.example.huixue.mapper;

import com.example.huixue.entity.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryMapper {
    
    /**
     * 查询所有分类
     */
    @Select("SELECT * FROM categories ORDER BY category_id")
    List<Category> selectAll();
    
    /**
     * 根据ID查询分类
     */
    @Select("SELECT * FROM categories WHERE category_id = #{categoryId}")
    Category selectById(Long categoryId);
    
    /**
     * 插入分类
     */
    @Insert("INSERT INTO categories (category_name, description, parent_id) VALUES (#{categoryName}, #{description}, #{parentId})")
    @Options(useGeneratedKeys = true, keyProperty = "categoryId")
    int insert(Category category);
    
    /**
     * 更新分类
     */
    @Update("UPDATE categories SET category_name = #{categoryName}, description = #{description}, parent_id = #{parentId} WHERE category_id = #{categoryId}")
    int update(Category category);
    
    /**
     * 删除分类
     */
    @Delete("DELETE FROM categories WHERE category_id = #{categoryId}")
    int delete(Long categoryId);
    
    /**
     * 根据父分类ID查询子分类
     */
    @Select("SELECT * FROM categories WHERE parent_id = #{parentId} ORDER BY category_id")
    List<Category> selectByParentId(Long parentId);
    
    /**
     * 查询顶级分类（没有父分类的）
     */
    @Select("SELECT * FROM categories WHERE parent_id IS NULL ORDER BY category_id")
    List<Category> selectTopLevel();
} 